package cn.com.headfree.file.service.impl;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;

import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.callback.call.FileServerCall;
import cn.com.headfree.entity.TCommonFile;
import cn.com.headfree.file.service.ICommonFileService;
import cn.com.headfree.file.service.IFileService;
import cn.com.headfree.vo.file.DealFileVo;
import feign.Response;

@Service("commonFileService")
public class CommonFileServiceImpl implements ICommonFileService {
	private static final Logger LOG = LogManager.getLogger(CommonFileServiceImpl.class);
	@Autowired
	private FileServerCall fileServerCall;
	@Autowired
    public IFileService fileService;

	@Override
	public ResultData upload(MultipartFile file, TCommonFile commonFile) throws Exception {

		ResultData result = new ResultData();

//		try {
//			result = fileServerCall.upload(file, commonFile);
//			LOG.info("human-manage调用公共组件文件服务接口返回数据为--" + JSON.toJSONString(result));
//		} catch (Exception e) {
//			e.printStackTrace();
//			LOG.error("human-manage调用公共组件文件服务接口出现异常", e);
//			result.setSuccess(false);
//			result.setErrorMsg("human-manage调用公共组件文件服务接口出现异常");
//			throw new Exception("human-manage调用公共组件文件服务接口出现异常");
//		}
		return result;
	}

	@Override
	public Response download(String fileId) throws Exception {
		ResultData result = new ResultData();

		try {
			Response response = fileServerCall.downloadFile(fileId);
//			LOG.info("human-manage调用公共组件文件服务接口返回数据为--" + JSON.toJSONString(response));
			return response;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("human-manage调用公共组件文件服务接口出现异常", e);
			result.setSuccess(false);
			result.setErrorMsg("human-manage调用公共组件文件服务接口出现异常");
			throw new Exception("human-manage调用公共组件文件服务接口出现异常");
		}
	}

	@Override
	public ResultData findFileList(String sysCode, String businessId, String businessNode) {
		ResultData result = new ResultData();
		try {
			result = fileServerCall.findFileList(sysCode,businessId,businessNode);
			LOG.info("human-manage调用公共组件文件服务接口返回数据为--" + JSON.toJSONString(result));
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("human-manage调用公共组件文件服务接口出现异常", e);
			result.setSuccess(false);
			result.setErrorMsg("human-manage调用公共组件文件服务接口出现异常");
		}
		return result;
	}

	@Override
	public ResultData deleteFile(String fileId) {
		ResultData result = new ResultData();
		try {
			result = fileServerCall.deleteFile(fileId);
			LOG.info("human-manage调用公共组件文件服务接口返回数据为--" + JSON.toJSONString(result));
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("human-manage调用公共组件文件服务接口出现异常", e);
			result.setSuccess(false);
			result.setErrorMsg("human-manage调用公共组件文件服务接口出现异常");
		}
		return result;
	}

	@Override
	public ResultData getFilesByBusinessId(String businessId) {
		ResultData data=new ResultData();
		try {
			List<DealFileVo> list=fileService.getFilesByBusinessId(businessId);
			data.setData(list);
		} catch (Exception e) {
			data.setSuccess(false);
			data.setErrorMsg("getFilesByBusinessId调用失败");
			e.printStackTrace();
		}
		return data;
	}

}
